package com.yanzhen.controller;

import com.github.pagehelper.PageInfo;
import com.yanzhen.po.User;
import com.yanzhen.po.UserRole;
import com.yanzhen.service.IUserService;
import com.yanzhen.util.JsonObject;
import com.yanzhen.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 查询所有的用户信息并进行分页处理
     * @return
     * http://localhost:8080/user/userAll 查看打印数据   在线的json处理
     */
    @RequestMapping("user/userAll")
    @ResponseBody  //                                       如果没有数据 默认首页为1                           默认一页数据为10条
    public JsonObject queryUserInfo(Model model, User user, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")Integer limit){
        JsonObject object=new JsonObject();
        PageInfo<User> pageInfo=userService.findUserAll(page,limit,user);
        System.out.println(pageInfo);
//        包装成前端所需要的数据
        object.setCode(0);
        object.setCount(pageInfo.getTotal());
        object.setData(pageInfo.getList());
        object.setMsg("ok");
        return object;
    }

    /**
     * 菜单的转发
     */
    @RequestMapping("/addUser")
    public String addUser(){
        return "pages/addUser";
    }
    /**
     * 添加功能
     * @return
     */
    @ResponseBody
    @RequestMapping("user/addUserSubmit")
    public R addUserSubmit(@RequestBody User user){  //@RequestBody  以json的格式接受
//        System.out.println(user);
        user.setCreate_time(new Date());
        userService.addUser(user);
        return R.ok();
    }
    /**
     * 修改菜单的映射
     */
    @RequestMapping("/updateUser")
    public String updateUser(Integer id,Model model){
        // 先根据用户的id查询用户信息
        User user=userService.queryUserById(id);
        model.addAttribute("user",user);
        System.out.println(user);
        return "pages/updateUser";
    }
    /**
     * 修改用户密码
     */
    @RequestMapping("/updateUserPwd")
    public String updateUser(){
        return "pages/updatePwd";
    }
    /**
     * 修改功能
     * @return
     */
    @ResponseBody
    @RequestMapping("user/updateUserSubmit")
    public R updateUserSubmit(@RequestBody User user){
        System.out.println(user);
        userService.updateUserSubmit(user);
        return R.ok();
    }
    /**
     * 删除功能
     * @return
     */
    @ResponseBody
    @RequestMapping("user/deleteUserByIds")
    public R deleteUserByIds(String ids){
        List list= Arrays.asList(ids.split(","));  //数组变为list集合
        userService.deleteUserInfoByIds(list);
        return R.ok();
    }


    @RequestMapping("user")
    public String UserIndex(){
        return "pages/user";
    }

}
